/*
 * @Author: weiwang
 * @Date:   2018-12-02 13:14:26
 * @Last Modified by:   weiwang
 * @Last Modified time: 2018-12-02 13:14:30
 */
'use strict';

const Service = require('egg').Service;

class WeappUsersService extends Service {
  // 该账号是否已经注册
  async wxHasRegister(openId) {
    const users = await this.ctx.model.Users.findOne({
      where: { openId },
    });
    if (users && users.get('openId')) {
      return users;
    }
    return false;
  }

  // 新用户注册
  async wxRegister(usersAccount) {
    const usersInfo = await this.ctx.model.Users.create(usersAccount);
    return usersInfo.get({
      plain: true,
    });
  }

  // 用户信息
  async userInfo(id) {
    const usersInfo = await this.ctx.model.Users.findOne({
      where: { id },
    });
    if (!usersInfo) {
      this.ctx.throw(404, '用户不存在');
    }
    return usersInfo;
  }

  // 设置用户信息
  async setUserInfo(id, usersAccount) {
    await this.ctx.model.Users.update(usersAccount, { where: { id } });
  }
}

module.exports = WeappUsersService;